package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.mapper.VillageMapper;
import com.community.pojo.dto.VillageQueryDto;
import com.community.pojo.entity.LayuiResult;
import com.community.pojo.entity.Result;
import com.community.pojo.entity.Village;
import com.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jdk.jfr.Percentage;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Limit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/village")
@Tag(name = "小区相关接口",description = "小区相关api")
public class VillageController {
    @Autowired
    VillageService villageService;

    @GetMapping("/list")//配置请求路径
    @Tool(description = "查询所有Village")
    public Result list(@ToolParam VillageQueryDto villageQueryDto) throws Exception {

        //1.设置分页条件
        PageHelper.startPage(villageQueryDto.getPage(),villageQueryDto.getLimit());


        //2.构建查询条件
        LambdaQueryWrapper<Village> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        if(!StringUtils.isEmpty(villageQueryDto.getName())){
            lambdaQueryWrapper.like(Village::getName,villageQueryDto.getName());
        }

        if(!StringUtils.isEmpty(villageQueryDto.getNumber())){
            lambdaQueryWrapper.eq(Village::getNumber,villageQueryDto.getNumber());
        }

        //3.查询
        List<Village> villageList = villageService.list(lambdaQueryWrapper);

        //4.获取分页结果
        PageInfo<Village> pageInfo=new PageInfo<>(villageList);

        //5.封装返回结果
        return Result.buildSuccess(pageInfo.getList(),pageInfo.getTotal());
    }


    @GetMapping("/findAll")//配置请求路径
    @Tool(description = "查询所有Village")
    public Result findAll() throws Exception {

        //1.查询所有小区
        List<Village> villageList = villageService.list();

        //2.封装返回结果
        return Result.buildSuccess(villageList);
    }

    @GetMapping("/find")
    @Tool(description = "根据id查询village")
    public Result find(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.根据id查询小区
        Village village = villageService.getById(id);

        //2.封装返回结果
        return Result.buildSuccess(village);
    }


    @PostMapping("/add")
    @Tool(description = "添加village")
    public Result add(@ToolParam @RequestBody Village village){//@RequestBody 表示接收Json数据,底层就是利用jackson把json类型的字符串转换成javabean

        village.setCreated(LocalDateTime.now());
        //1.保存数据
        villageService.save(village);

        //2.返回结果
        return Result.buildSuccess();
    }

    @PostMapping("/update")
    @Tool(description = "更新village")
    public Result update(@ToolParam @RequestBody Village village){
        log.info(village.getImage());
        //1.保存数据
        villageService.updateVillage(village);

        //2.返回结果
        return Result.buildSuccess();
    }


    @GetMapping("/delete")
    @Tool(description = "根据id删除village")
    public Result delete(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.根据id查询小区
        villageService.removeById(id);

        //2.封装返回结果
        return Result.buildSuccess();
    }


}
